package org.siam.impl.schemas {
	
	import org.siam.core.schemas.ISchema;
	import org.siam.core.schemas.properties.ISchemaProperty;
	import org.siam.impl.SiamManager;
	import org.siam.impl.schemas.properties.SchemaPropertyXMLHandler;
	import org.siam.utils.RuntimeError;
	
	public class SchemaXMLHandler {
		
		public static function read(input:XML, siam:SiamManager):ISchema {
			var schema:Schema = new Schema();
			var schemaId:String = input["@id"];
			schema._id = schemaId;
			for each (var propertyXML:XML in input["property"]) {
				var property:ISchemaProperty = SchemaPropertyXMLHandler.read(propertyXML, schemaId, siam);
				var propertyId:String = property.id;
				if (!propertyId)
					throw new RuntimeError("A property must have a valid id for schema '{0}'", schemaId);
				else if (schema.hasProperty(propertyId)) {
					throw new RuntimeError("Duplicated property id '{0}' for schema '{1}'", propertyId, schemaId);
				}
				schema.addProperty(property);
			}
			return schema;
		}
	}
}